from es import tell, exists, getplayername, getplayersteamid
from cmdlib import registerClientCommand, unregisterClientCommand, registerSayCommand, unregisterSayCommand
from popuplib import create, easymenu, send, find
from usermsg import hudhint
from wcs.wcs import getPlayer, admin, dataAPI, CfgManager


cfg = CfgManager('gold')


cfg.text("""
// ****************************
// ****************************
// ******* GOLD SETTINGS ******
// ****************************
// ****************************""", False)

gain		= cfg.cvar('wcs_gold_gain',		15,		'Gold gained for every kill')
headshot	= cfg.cvar('wcs_gold_headshot',	5,		'Gold gained for every headshot')
knife		= cfg.cvar('wcs_gold_knife',	5,		'Gold gained for every knife kill')

cfg.write()

db = dataAPI.DB('gold')


def load():
	cfg.execute()

	registerClientCommand('gold', register, '')
	registerClientCommand('goldadmin', register1, '')
	registerClientCommand('goldshop', register2, '')
	registerSayCommand('gold', register, '')
	registerSayCommand('goldadmin', register1, '')
	registerSayCommand('goldshop', register2, '')

	db.load()

def unload():
	unregisterClientCommand('gold')
	unregisterClientCommand('goldadmin')
	unregisterClientCommand('goldshop')
	unregisterSayCommand('gold')
	unregisterSayCommand('goldadmin')
	unregisterSayCommand('goldshop')

	db.save()

def es_map_start(ev):
	db.save()

def register(userid, args):
	hudhint(userid, 'You got currently '+str(db[getplayersteamid(userid)] if getplayersteamid(userid) in db else 0)+' gold')

def register1(userid, args):
	if admin.getPlayer(userid).hasFlag('goldadmin'):
		pass
	else:
		tell(userid, '#multi', '#lightgreenYou\'re #greennot #lightgreenan Gold-bank admin')

def register2(userid, args):
	pass


def player_death(ev):
	attacker = str(ev['attacker'])

	if not str(ev['userid']) == attacker:
		if not ev['es_userteam'] == ev['es_attackerteam']:
			steamid = getplayersteamid(attacker)
			if not steamid in db:
				db[steamid] = [0,0]

			if int(ev['headshot']):
				hudhint(attacker, 'You gained '+str(int(gain)+int(headshot))+' gold for making a headshot')
				db[steamid][0] += int(gain)+int(headshot)
			elif ev['weapon'] == 'knife':
				hudhint(attacker, 'You gained '+str(int(gain)+int(knife))+' gold for making a knife kill')
				db[steamid][0] += int(gain)+int(knife)
			else:
				hudhint(attacker, 'You gained '+str(gain)+' gold for making a kill')
				db[steamid][0] += int(gain)

			db[steamid][1] += 1
